import random
import uuid
from datetime import datetime, timedelta
from confg import constants


def generate_user_preferences():
    """生成随机用户偏好"""
    # 随机选择1-3种口味
    flavors = random.sample(constants.FLAVORS, k=random.randint(1, 3))

    # 随机选择1-2种类别
    categories = random.sample(constants.CATEGORIES, k=random.randint(1, 2))

    # 健康约束 (30%概率没有约束)
    if random.random() < 0.7:
        health_constraints = random.sample(constants.HEALTH_CONSTRAINTS[1:], k=random.randint(1, 2))
    else:
        health_constraints = ["无"]

    # 价格范围
    price_range = random.choice(constants.PRICE_RANGES)

    return {
        "flavor_profile": flavors,
        "category_pref": categories,
        "health_constraints": health_constraints,
        "price_range": price_range
    }


def generate_recommendation_ticket(recommender, user_id, timestamp):
    """生成推荐工单"""
    # 生成用户偏好
    preferences = generate_user_preferences()


    # 获取推荐结果
    recommendations, user_cluster = recommender.recommend(preferences, user_id)


    # 创建工单
    ticket = {
        "ticket_id": f"TK{str(uuid.uuid4())[:8].upper()}",
        "user_id": user_id,
        "request_time": timestamp.strftime("%Y-%m-%d %H:%M:%S"),
        "preferences": preferences,
        "ml_metadata": {
            "user_cluster": f"CLUSTER_{user_cluster}",
            "model_version": "SNACK-REC-v2.1",
            "processing_time_ms": random.randint(50, 250)
        },
        "recommendations": recommendations[:3]  # 最多3条推荐
    }


    # 添加状态字段
    ticket["status"] = "SUCCESS" if recommendations else "NO_MATCH"


    return ticket



def generate_user_ids(num_users=200):
    """生成用户ID列表"""
    return [f"U{10000 + i}" for i in range(num_users)]


def generate_timestamps(num_records=200, start_days_ago=30):
    """生成时间序列"""
    base_time = datetime.now() - timedelta(days=start_days_ago)
    return [base_time + timedelta(minutes=i * 15) for i in range(num_records)]